let Vue;

class Store {
  constructor(options) {
    const computed={}
    this.getters={}

    this._wrapperGetters=options.getters

    const store=this

    Object.keys(store._wrapperGetters).forEach(key=>{
      computed[key]=function (){
        return store._wrapperGetters[key](store.state)
      }
      Object.defineProperty(store.getters,key,{
        get(){
          return store.state[key]
        }
      })
    })


    this.state=new Vue({
      data(){
        return options.state
      },
      computed
    })
    this.commit=this.commit.bind(this)
    this.dispatch=this.dispatch.bind(this)
    this.$mutations=options.mutations
    this.$actions=options.actions
  }
  commit(type,payload){
    const entry=this.$mutations[type]
    entry(this.state,payload)
  }
  dispatch(type,payload){
    const entry=this.$actions[type]
    entry(this,payload)
  }
}

function install(_vue) {
  Vue = _vue;
  Vue.mixin({
    beforeCreate() {
      if (this.$options.store) {
        Vue.prototype.$store = this.$options.store
      }
    }
  })
}

export default {Store, install}
